//
//  TJTabBar.m
//  TJSinaWeiBo_OC
//
//  Created by 唐后能 on 16/4/13.
//  Copyright © 2016年 itheima. All rights reserved.
//

#import "TJTabBar.h"

@interface TJTabBar ()

@property (nonatomic, strong) UIButton *addBtn;

@end

@implementation TJTabBar
-(instancetype)initWithFrame:(CGRect)frame{

    if ([super initWithFrame:frame]) {
        [self setupUI];
    }
    return self;
}
- (instancetype)initWithCoder:(NSCoder *)aDecoder {

    if ([super initWithCoder:aDecoder]) {
        [self setupUI];
    }
    return self;
}

- (void)setupUI {
    
    // 1,添加加号按钮.
    self.addBtn = [[UIButton alloc] init];
    [self.addBtn setBackgroundImage:[UIImage imageNamed:@"tabbar_compose_button"] forState:UIControlStateNormal];
    [self.addBtn setBackgroundImage:[UIImage imageNamed:@"tabbar_compose_button_highlighted"] forState:UIControlStateHighlighted];
    [self.addBtn setImage:[UIImage imageNamed:@"tabbar_compose_icon_add"] forState:UIControlStateNormal];
    [self.addBtn setImage:[UIImage imageNamed:@"tabbar_compose_icon_add_highlighted"] forState:UIControlStateHighlighted];
//    [self.addBtn sizeToFit];
    // 添加监听事件.
    [self.addBtn addTarget:self action:@selector(addBtnClicked) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:self.addBtn];
}

// 重新布局子控件.
-(void)layoutSubviews {

    [super layoutSubviews];
//    NSLog(@"subViews = %@",self.subviews);
    int index = 0;
    CGFloat width = self.bounds.size.width / 5;
    // 重新布局原来四个UITabBarButton
    for (UIView* subView in self.subviews) {
        if ([subView isKindOfClass:NSClassFromString(@"UITabBarButton")]) {
            subView.frame = CGRectMake(index*width, 0, width, self.bounds.size.height);
            index += index==1 ? 2 : 1;
        }
    }
    // 布局加号按钮.
    self.addBtn.frame = CGRectMake(2*width, 0, width, self.bounds.size.height);
}
#pragma mark - 按钮点击事件.
- (void) addBtnClicked {

    NSLog(@"点击了加号按钮");
}

@end
